package com.zhanglong.kotlinfish

import android.content.Intent
import kotlinx.android.synthetic.main.activity_fish.*

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.chibatching.kotpref.Kotpref
import com.chibatching.kotpref.KotprefModel
import com.zhanglong.kotlinfish.Util.HardWare.BGMPlayerUtil
import com.zhanglong.kotlinfish.Util.System.VersionUtil
import com.zhanglong.kotlinfish.Util.Web.JSBridgeUtil
import com.zhanglong.kotlinfish.Util.Web.WebViewSettingUtil
import org.jetbrains.anko.toast


class FishActivity : AppCompatActivity() {

    lateinit var webViewUtil: WebViewSettingUtil
    lateinit var bgmPlayerUtil: BGMPlayerUtil
    lateinit var jsBridgeUtil: JSBridgeUtil

    object MusicControl : KotprefModel() {
        var isMusicOff by booleanPref()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        webViewUtil.onActivityResult(requestCode,resultCode,data)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fish)

        utilityInit()
        webViewUtil.loadUrl("http://game.myfish.io")
        var version = VersionUtil.versionName(this)

    }

    override fun onPause() {
        bgmPlayerUtil.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        if (!MusicControl.isMusicOff)
        {
            bgmPlayerUtil.play()
        }
    }

    private fun utilityInit() {
        Kotpref.init(this)

        webViewUtil = WebViewSettingUtil(this,webView, callBack = object : WebViewSettingUtil.WebSettingCallBack {
            override fun onSuccess() {
                toast("加载成功")
            }

            override fun onFaild() {
                webView.visibility = View.INVISIBLE
                toast("加载失败")
            }
        })
        webViewUtil.userDefaultSetting(R.drawable.welcome)
        bgmPlayerUtil = BGMPlayerUtil(this, R.raw.bgm)
        jsBridgeUtil = JSBridgeUtil(webView,callBack = object : JSBridgeUtil.JSBridgeCallBack{
            override fun jsHandle(cmd: String, param: String, ext: String) {
                musicControl(cmd, param)
            }
        })
    }

    // JS - Brige Controll
    private fun musicControl(cmd: String, param: String) {
        if (cmd != "Music") return
        if (param == "on") {
            actionMusicOnAck()
        } else if (param == "off") {
            actionMusicOffAck()
        }
    }
    private fun actionMusicOnAck() {
        bgmPlayerUtil.play()
        jsBridgeUtil.evaluateJS("MusicAck", "on", "")
        MusicControl.isMusicOff = false
    }
    private fun actionMusicOffAck() {
        if (bgmPlayerUtil.isPlaying()) {
            bgmPlayerUtil.pause()
            jsBridgeUtil.evaluateJS("MusicAck", "off", "")
            MusicControl.isMusicOff = true
        } else {
            jsBridgeUtil.evaluateJS("MusicAck", "on", "")
            MusicControl.isMusicOff = false
        }
    }
}